package cn.hnucm.serchhbservice.controller;


import cn.hnucm.serchhbservice.dao.TbHbBasisDao;
import cn.hnucm.serchhbservice.entity.TbHbBasis;
import cn.hnucm.serchhbservice.entity.TbHbRelation;
import cn.hnucm.serchhbservice.service.TbHbBasisService;
import cn.hnucm.serchhbservice.service.TbHbRelationService;
import cn.hnucm.serchhbservice.utils.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import org.apache.lucene.analysis.Analyzer;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjl
 * @since 2021-12-09
 */
@Api(tags = "tb_Hb_Basis类")
@RestController
@RequestMapping("/serchhbservice/tbHbBasis")
public class TbHbBasisController {

    @Autowired
    private TbHbBasisService tbHbBasisService;
    @Autowired
    private TbHbRelationService tbHbRelationService;
    @Autowired
    private TbHbBasisDao tbHbBasisDao;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 路径： /serchhbservice/tbHbBasis/getHbBasis/1
     * 描述: 根据hbId查询tb_hb_basis表
     *
     * @param hbId 皇榜id
     * @return Result
     */

    @GetMapping("/getHbBasis/{hbId}")
    public List<TbHbBasis> getHbBasisById(@PathVariable("hbId") int hbId) {

        QueryWrapper<TbHbBasis> tbHbBasisQueryWrapper = new QueryWrapper<>();
        tbHbBasisQueryWrapper.eq("hbId", hbId);
        return tbHbBasisService.list(tbHbBasisQueryWrapper);
    }

    /**
     * 路径： /serchhbservice/tbHbBasis/addHbBasis
     * 描述: 向tb_hb_basis表添加HbBasis
     *
     * @param tbHbBasis 皇榜基本信息
     * @return boolean
     */

    @PostMapping("/addHbBasis")
    public boolean addHbBasis(@RequestBody TbHbBasis tbHbBasis) {
        tbHbBasis.setHbReleaseDatetime(dateFormat.format(new Date(System.currentTimeMillis())));
        boolean bool = tbHbBasisService.addTbHbBasis(tbHbBasis);
        tbHbBasisDao.save(tbHbBasis);
        TbHbRelation tbHbRelation = new TbHbRelation();
        tbHbRelation.setHbId(tbHbBasis.getHbId());
        tbHbRelationService.save(tbHbRelation);
        return bool;
    }

    /**
     * 路径： /serchhbservice/tbHbBasis/updateHbBasis
     * 描述: 根据hbId更新tb_hb_basis表的数据
     *
     * @param tbHbBasis 皇榜基本信息
     * @return boolean
     */

    @PostMapping("/updateHbBasis")
    public boolean updateHbBasis(@RequestBody TbHbBasis tbHbBasis) {
        boolean bool = tbHbBasisService.updateById(tbHbBasis);
        tbHbBasisDao.save(tbHbBasis);
        return bool;
    }

    /**
     * 路径： /serchhbservice/tbHbBasis/deleteHbBasis
     * 描述: 根据hbId删除tb_hb_basis表的数据
     *
     * @param hbId 皇榜id
     * @return boolean
     */

    @PostMapping("/deleteHbBasis")
    public boolean deleteHbBasis(@RequestBody int hbId) {
        boolean bool = tbHbBasisService.removeById(hbId);
        TbHbBasis tbHbBasis = new TbHbBasis();
        tbHbBasis.setHbId(hbId);
        tbHbBasisDao.delete(tbHbBasis);
        return bool;
    }

    /**
     * 模糊查询
     */
//    @GetMapping("/searchHbBasisListByName/{keyword}")
//    public List<TbHbBasis> searchHbBasisListByName(@PathVariable("keyword") String keyword) {
//        try {
//            keyword = URLDecoder.decode(keyword, "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//
//        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("hbName", keyword).fuzziness(Fuzziness.AUTO);
//        Iterable<TbHbBasis> tbHbBases = tbHbBasisDao.search(fuzzyQueryBuilder);
//        List<TbHbBasis> tbHbBasisList = new ArrayList<>();
//        for (TbHbBasis tbHbBasis : tbHbBases) {
//            tbHbBasisList.add(tbHbBasis);
//        }
//        return tbHbBasisList;
//    }
//
//    /**
//     * 根据类别查找
//     *
//     * @param Type
//     * @return
//     */
//    @GetMapping("/searchHbBasisListByType/{Type}")
//    public List<TbHbBasis> searchHbBasisListByType(@PathVariable("Type") String Type) {
//        try {
//            Type = URLDecoder.decode(Type, "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("hbType", Type);
//        Iterable<TbHbBasis> tbHbBases = tbHbBasisDao.search(termQueryBuilder);
//        List<TbHbBasis> tbHbBasisList = new ArrayList<>();
//        for (TbHbBasis tbHbBasis : tbHbBases) {
//            tbHbBasisList.add(tbHbBasis);
//        }
//        return tbHbBasisList;
//    }

    @GetMapping("/getAllHbBasisList")
    public List<TbHbBasis> getAllHbBasisList() {
        Iterable<TbHbBasis> tbHbBases = tbHbBasisDao.findAll();
        List<TbHbBasis> tbHbBasisList = new ArrayList<>();
        for (TbHbBasis tbHbBasis : tbHbBases) {
            tbHbBasisList.add(tbHbBasis);
        }
        return tbHbBasisList;
    }

    @GetMapping("/getAllHbBasisList/{currentPage}/{pageSize}")
    public List<TbHbBasis> getAllHbBasisList(@PathVariable("currentPage") int currentPage, @PathVariable("pageSize") int pageSize) {
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
        Page<TbHbBasis> tbHbBases = tbHbBasisDao.findAll(pageRequest);
        List<TbHbBasis> tbHbBasisList = new ArrayList<>();
        for (TbHbBasis tbHbBasis : tbHbBases) {
            tbHbBasisList.add(tbHbBasis);
        }
        return tbHbBasisList;
    }

    @GetMapping("/searchHbBasisList/{key}/{value}/{lowPrice}/{highPrice}")
    public List<TbHbBasis> searchHbBasisList(
            @PathVariable("key") String key, @PathVariable("value") String value,
            @PathVariable("lowPrice") int low, @PathVariable("highPrice") int high) {

        try {
            key = URLDecoder.decode(key, "UTF-8");
            value = URLDecoder.decode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        FuzzyQueryBuilder fuzzyQueryBuilder;
        if (key == "hbType") {
            fuzzyQueryBuilder = QueryBuilders.fuzzyQuery(key, value).fuzziness(Fuzziness.ZERO);
        } else {
            fuzzyQueryBuilder = QueryBuilders.fuzzyQuery(key, value).fuzziness(Fuzziness.ONE);
        }
        QueryBuilder query = QueryBuilders.boolQuery()
                .must(fuzzyQueryBuilder)
                .must(QueryBuilders.rangeQuery("hbReward").from(low).to(high));
        Iterable<TbHbBasis> tbHbBases = tbHbBasisDao.search(query);
        List<TbHbBasis> tbHbBasisList = new ArrayList<>();
        for (TbHbBasis tbHbBasis : tbHbBases) {
            tbHbBasisList.add(tbHbBasis);
        }
        return tbHbBasisList;
    }

    @GetMapping("/searchHbBasisList/{key}/{value}/{lowPrice}/{highPrice}/{currentPage}/{pageSize}")
    public List<TbHbBasis> searchHbBasisList(
            @PathVariable("key") String key, @PathVariable("value") String value,
            @PathVariable("lowPrice") int low, @PathVariable("highPrice") int high,
            @PathVariable("currentPage") int currentPage, @PathVariable("pageSize") int pageSize) {
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
        try {
            key = URLDecoder.decode(key, "UTF-8");
            value = URLDecoder.decode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        FuzzyQueryBuilder fuzzyQueryBuilder;
        if (key == "hbType") {
            fuzzyQueryBuilder = QueryBuilders.fuzzyQuery(key, value).fuzziness(Fuzziness.ZERO);
        } else {
            fuzzyQueryBuilder = QueryBuilders.fuzzyQuery(key, value).fuzziness(Fuzziness.ONE);
        }
        QueryBuilder query = QueryBuilders.boolQuery()
                .must(fuzzyQueryBuilder)
                .must(QueryBuilders.rangeQuery("hbReward").from(low).to(high));
        Iterable<TbHbBasis> tbHbBases = tbHbBasisDao.search(query,pageRequest);
        List<TbHbBasis> tbHbBasisList = new ArrayList<>();
        for (TbHbBasis tbHbBasis : tbHbBases) {
            tbHbBasisList.add(tbHbBasis);
        }
        return tbHbBasisList;
    }

}

